package com.xing.crm.service.impl;

import com.xing.crm.mapper.ProCategoryMapper;
import com.xing.crm.pojo.PageResult;
import com.xing.crm.pojo.ProCategoryPoJo;
import com.xing.crm.pojo.Result;
import com.xing.crm.service.ProCategoryService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Mr.L
 * @version V1.0
 * @date 2021年07月21日 17:37
 */
@Transactional(rollbackFor={Exception.class})
@Service
public class ProCategoryServiceImpl implements ProCategoryService
{
	@Resource
	private ProCategoryMapper proCategoryMapper;
	
	//列表数据后台
	@Override
	public PageResult<ProCategoryPoJo> list(ProCategoryPoJo proCategoryPoJo, int page, int limit)
	{
		Map<String, Object> params = new HashMap(0);
		params.put("category", proCategoryPoJo);
		params.put("start", (page - 1) * limit);
		params.put("limit", limit);
		List<ProCategoryPoJo> poJo = this.proCategoryMapper.findList(params);
		int total = this.proCategoryMapper.count(params).intValue();
		PageResult<ProCategoryPoJo> result = new PageResult();
		result.setCode(0);
		result.setCount(total);
		result.setMsg("查询成功");
		result.setData(poJo);
		
		return result;
	}
	
	//列表数据前台展示的，因为有个是否显示的条件，所以多弄了一份
	@Override
	public PageResult<ProCategoryPoJo> listForWeb(ProCategoryPoJo proCategoryPoJo, int page, int limit)
	{
		Map<String, Object> params = new HashMap(0);
		params.put("category", proCategoryPoJo);
		params.put("start", (page - 1) * limit);
		params.put("limit", limit);
		List<ProCategoryPoJo> poJo = this.proCategoryMapper.findListFoeWeb(params);
		int total = this.proCategoryMapper.count(params).intValue();
		PageResult<ProCategoryPoJo> result = new PageResult();
		result.setCode(0);
		result.setCount(total);
		result.setMsg("查询成功");
		result.setData(poJo);
		
		return result;
	}
	
	//列表数据，全部出来，没有分页的，应该是用于下拉框的
	@Override
	public List<ProCategoryPoJo> findAllCategories()
	{
		return this.proCategoryMapper.findAllCategories();
	}
	
	//新增、编辑
	@Override
	public Map<String, Object> edit(ProCategoryPoJo proCategoryPoJo)
	{
		Map<String, Object> result = new HashMap(0);
		result.put("code", Integer.valueOf(0));
		int success;
		if (proCategoryPoJo.getId() == null) {
			success = this.proCategoryMapper.add(proCategoryPoJo);
		} else {
			success = this.proCategoryMapper.edit(proCategoryPoJo);
		}
		result.put("data", Integer.valueOf(success));
		result.put("msg", success > 0 ? "保存成功" : "操作失败");
		return result;
	}
	
	//得到一个数据
	@Override
	public ProCategoryPoJo get(Integer id)
	{
		ProCategoryPoJo proCategoryPoJo = null;
		try
		{
			proCategoryPoJo = this.proCategoryMapper.get(id);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return proCategoryPoJo;
	}
	
	//修改排序
	@Override
	public Result sort(ProCategoryPoJo proCategoryPoJo){
		Result result = new Result();
		int affectedRows = 0;
		if (proCategoryPoJo.getId() != null) {
			affectedRows = this.proCategoryMapper.sort(proCategoryPoJo);
		}
		result.setCode(affectedRows > 0 ? 0 : 1);
		result.setMsg(affectedRows > 0 ? "修改成功" : "没有执行成功");
		return result;
	}
	
	//切换显示
	@Override
	public Result toggleShow(Integer id){
		Result result = new Result();
		int affectedRows = 0;
		if (id != null) {
			affectedRows = this.proCategoryMapper.toggleShow(id);
		}
		result.setCode(affectedRows > 0 ? 0 : 1);
		result.setMsg(affectedRows > 0 ? "修改成功" : "没有执行成功");
		return result;
	}
	
}
